package cn.micro.system.manage.annotate.impl;


import cn.micro.system.manage.annotate.OldPasswordIsTrue;
import cn.micro.system.manage.entity.dto.UpdatePasswordDTO;
import cn.micro.system.manage.service.SysDimUserService;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class UpdatePasswordValidation <T extends Annotation> implements ConstraintValidator<T, UpdatePasswordDTO> {

    protected Predicate<UpdatePasswordDTO> predicate = c -> true;

    @Resource
    protected SysDimUserService sysDimUserService;

    @Override
    public boolean isValid(UpdatePasswordDTO dto, ConstraintValidatorContext constraintValidatorContext) {
        return sysDimUserService == null || predicate.test(dto);
    }

    /**
     * 校验角色编码或者角色名称是否重复
     */
    public static class OldPasswordIsTrueValidate extends UpdatePasswordValidation<OldPasswordIsTrue>{
        @Override
        public void initialize(OldPasswordIsTrue constraintAnnotation) {
            predicate = c -> !sysDimUserService.getOldPasswordIsTrue(c);
        }
    }

}
